﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Ipc;
using System.Text;

namespace SimpleIpc
{
    public class IpcRemoteChannel : AbstractRemoteChannel
    {
        private string name;

        public override string Name => name;
        public IpcRemoteChannel(string name)
            : this(name, string.Empty)
        {

        }
        /// <summary>
        /// ipc信道
        /// </summary>
        /// <param name="name">信道名称</param>
        /// <param name="portName">ipc端口名称</param>
        public IpcRemoteChannel(string name, string portName)
        {
            this.name = name;
            IDictionary props = new Hashtable();
            props["name"] = name;
            if (!string.IsNullOrEmpty(portName))
            {
                props["portName"] = portName;
            }
            CreateChannel(props, new BinaryClientFormatterSinkProvider(), new BinaryServerFormatterSinkProvider());
        }

        public IpcRemoteChannel(IDictionary properties, IClientChannelSinkProvider clientSinkProvider, IServerChannelSinkProvider serverSinkProvider)
        {
            CreateChannel(properties, clientSinkProvider, serverSinkProvider);
        }

        private void CreateChannel(IDictionary properties, IClientChannelSinkProvider clientSinkProvider, IServerChannelSinkProvider serverSinkProvider)
        {
            try
            {
                channel = new IpcChannel(properties, clientSinkProvider, serverSinkProvider);
            }
            catch (Exception)
            {
                throw;
            }

        }
    }
}
